import java.util.*;

public class Test1 {


    public static void main(String[] args) {
         String str1 = "zfc";
         String str3 = new String("zfc");
        System.out.println(str1 == str3);//输出false



    }

    public List<String> topKFrequent(String[] words, int k) {
        //1.统计每个单词出现的次数
        Map<String , Integer> map = new HashMap<>() ;
        for(String str : words){
            if(map.containsKey(str)){
                int val = map.get(str);
                map.put(str,val+1);
            }else{
                map.put(str,1);
            }
        }
        //2。建立小根堆，用小根堆存放k个最大的单词
        //用单词出现的次数作为比较
        PriorityQueue<Map.Entry<String , Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            //传一个比较器，这是一个匿名内部类；用来比较val的大小
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                //如果个数val相同，建立大根堆
                if(o1.getValue().equals(o2.getValue())){
                    return o2.getKey().compareTo(o1.getKey());
                }

                //默认建立小根堆
                return o1.getValue() - o2.getValue();
            }
        });

        for(Map.Entry<String,Integer> entry : map.entrySet()){
            //如果栈里的元素个数小于k就把数据放进来
            if(minHeap.size()< k){
                minHeap.offer(entry);
            }else{
                Map.Entry<String,Integer> top = minHeap.peek();
                //应该entry的val等于栈顶的val就比较谁的key小，谁小要谁
                if(entry.getValue().equals(top.getValue())){
                    if(entry.getKey().compareTo(top.getKey()) < 0){
                       minHeap.poll();
                       minHeap.offer(entry);
                    }
                }else{
                    if(entry.getValue().compareTo(minHeap.peek().getValue()) > 0){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }

        List<String> list = new ArrayList<>();
        while (!minHeap.isEmpty()){
            Map.Entry<String,Integer> tmp = minHeap.poll();
            list.add(tmp.getKey());
        }
        //翻转list
        Collections.reverse(list);

 return list;
    }
}
